<template>
    <div class="user-info-container">
        <div class="card-container">
            <!-- 用户基本信息卡片 -->
            <el-card class="user-card">
                <div class="user-header">
                    <div class="avatar-container">
                        <img :src="userAvatar" alt="User Avatar" class="user-avatar">
                        <span class="avatar-badge"
                            :class="{ 'online': userInfo.state === 1, 'offline': userInfo.state === 0 }"></span>
                    </div>
                    <div class="user-basic-info">
                        <h2 class="user-nickname">{{ userInfo.nickname }}</h2>
                        <p class="user-username">{{ userInfo.username }}</p>
                        <div class="user-status">
                            <span
                                :class="{ 'online-text': userInfo.state === 1, 'offline-text': userInfo.state === 0 }">{{ userInfo.state === 1 ? '正常' : '未激活' }}</span>
                            <span class="register-date">注册于: {{ formatDateTime(userInfo.created_at) }}</span>
                        </div>
                    </div>
                </div>

                <div class="user-detail-info mt-15">
                    <div class="info-row">
                        <span class="info-label">ID:</span>
                        <span class="info-value">{{ userInfo.id }}</span>
                    </div>
                    <div class="info-row">
                        <span class="info-label">邮箱:</span>
                        <span class="info-value">{{ userInfo.email || '未设置' }}</span>
                    </div>
                    <div class="info-row">
                        <span class="info-label">手机:</span>
                        <span class="info-value">{{ userInfo.phone || '未设置' }}</span>
                    </div>
                </div>
            </el-card>

            <!-- 账户余额卡片 -->
            <el-card class="balance-card">
                <div class="balance-header">
                    <h3>账户余额</h3>
                    <button class="recharge-btn">充值</button>
                </div>
                <div class="balance-amount">
                    <span class="currency-sign">¥</span>
                    <span class="amount">{{ userInfo.balance.toFixed(2) }}</span>
                </div>
            </el-card>
        </div>

        <!-- 投注历史表格 -->
        <el-card class="bets-card">
            <div class="bets-header">
                <h3>投注历史</h3>
            </div>
            <el-table :default-sort="{ prop: 'batTime', order: 'descending' }" border height="400"
                v-if="userInfo.bets && userInfo.bets.length > 0" :data="userInfo.bets" style="width: 100%">
                <el-table-column prop="gameCode" label="期号" align="center" />
                <el-table-column prop="betType" label="玩法" align="center" />
                <el-table-column prop="result" label="投注方案">
                    <template #default="{ row }">
                        <div>本金：{{ row.betAmount }} 元 </div>
                        <div>倍数：{{ row.betCount }}倍</div>
                    </template>
                </el-table-column>
                <el-table-column width="170" prop="batTime" show-overflow-tooltip label="投注时间" align="center">
                    <template #default="{ row }">
                        {{ formatDateTime(new Date(row.batTime)) }}
                    </template>
                </el-table-column>
                <el-table-column prop="result" label="压注" align="center">
                </el-table-column>

                <el-table-column prop="betAmount" label="共投金额" align="center">
                    <template #default="{ row }">
                        {{ row.betAmount * row.betCount }}
                        元
                    </template>
                </el-table-column>
                <el-table-column width="150" label="是否中奖(注：未开奖时为未中奖)" align="center">
                    <template #default="{ row }">
                        <el-tag :type="row.isWin ? 'success' : 'danger'">{{ row.isWin ? '中奖金额' + row.winAmount + '元' : '未中奖'
                        }}</el-tag>
                    </template>
                </el-table-column>
            </el-table>
            <div v-else class="no-bets">
                <div class="empty-state">
                    <el-icon class="empty-icon">
                        <Document />
                    </el-icon>
                    <p class="empty-text">暂无投注记录</p>
                    <button class="start-betting-btn" @click="handleTo">去投注</button>
                </div>
            </div>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useJackpotStore } from '@/store';
import { storeToRefs } from 'pinia';
import { Document } from '@element-plus/icons-vue';
import { formatDateTime } from '@/utils/formart';
import { useRouter } from 'vue-router';
import { UserApi } from '@/api/user';

// 模拟用户头像
const userAvatar = ref('https://picsum.photos/200/200?random=1');

// 获取store中的用户信息
const store = useJackpotStore();
const router = useRouter();

const { userInfo, currentRouter } = storeToRefs(store);

const handleTo = () => {
    currentRouter.value = '/result'
    router.push('/result')
}

const { token } = storeToRefs(store);

if (token.value) {
    UserApi.getJackpotUserInfo()
        .then(res => {
            if (res.code === 200) {
                console.log(res.data)
                store.setUserInfo(res.data)
            }
        })
}
</script>

<style scoped lang="scss">
.user-info-container {
    padding: 0;
    background: linear-gradient(135deg, #f5f7fa 0%, #f0f2f5 100%);
    min-height: calc(100vh - 60px);
}

.card-container {
    display: flex;
    gap: 10px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}

.user-card {
    flex: 1;
    min-width: 300px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border-radius: 16px;
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    }
}

.user-header {
    display: flex;
    padding: 24px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    align-items: center;
    background: linear-gradient(to right, #ffffff, #f9f9f9);
}

.avatar-container {
    position: relative;
    margin-right: 24px;
}

.user-avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
    border: 4px solid #ffffff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;

    &:hover {
        transform: scale(1.05);
    }
}

.avatar-badge {
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid white;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
}

.online {
    background-color: #4caf50;
}

.offline {
    background-color: #ccc;
}

.user-basic-info {
    flex: 1;
}

.user-nickname {
    font-size: 20px;
    font-weight: bold;
    margin: 0 0 5px 0;
    color: #333;
}

.user-username {
    font-size: 14px;
    color: #666;
    margin: 0 0 10px 0;
}

.user-status {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 12px;
}

.online-text {
    color: #4caf50;
    display: flex;
    align-items: center;
}

.online-text::before {
    content: '●';
    font-size: 8px;
    margin-right: 5px;
}

.offline-text {
    color: #999;
    display: flex;
    align-items: center;
}

.info-row {
    display: flex;
    margin-bottom: 12px;
    font-size: 14px;
}

.info-row:last-child {
    margin-bottom: 0;
}

.info-label {
    width: 80px;
    color: #999;
}

.info-value {
    flex: 1;
    color: #333;
}

.balance-card {
    width: 300px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border-radius: 16px;
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    }
}

.balance-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #f0f0f0;
}

.balance-header h3 {
    font-size: 16px;
    margin: 0;
}

.recharge-btn {
    background-color: white;
    color: #1890ff;
    border: none;
    border-radius: 20px;
    padding: 6px 16px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    &:hover {
        background-color: rgba(255, 255, 255, 0.9);
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
}

.balance-amount {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px 0;
}

.currency-sign {
    font-size: 24px;
    margin-right: 5px;
}

.amount {
    font-size: 40px;
    font-weight: bold;
}

.balance-status {
    text-align: center;
    padding-bottom: 20px;
}

.status-text {
    font-size: 14px;
    color: #999;
}

.bets-card {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border-radius: 16px;
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;

    &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    }
}

.bets-header {
    padding: 20px 24px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    background: linear-gradient(to right, #ffffff, #f9f9f9);
}

.bets-header h3 {
    font-size: 18px;
    margin: 0;
    color: #333;
    font-weight: 600;
}

:deep(.el-table) {
    --el-table-border-color: rgba(0, 0, 0, 0.05);
    --el-table-header-bg-color: #f9f9f9;
    --el-table-tr-bg-color: transparent;

    th.el-table__cell {
        font-weight: 600;
        color: #555;
    }

    tr:hover td {
        background-color: rgba(24, 144, 255, 0.05) !important;
    }
}

.no-bets {
    padding: 60px 0;
    text-align: center;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.empty-icon {
    font-size: 60px;
    color: rgba(24, 144, 255, 0.2);
    margin-bottom: 20px;
}

.empty-text {
    font-size: 16px;
    color: #666;
    margin-bottom: 24px;
}

.start-betting-btn {
    background-color: #1890ff;
    color: white;
    border: none;
    border-radius: 24px;
    padding: 10px 24px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);

    &:hover {
        background-color: #40a9ff;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
    }
}

@media (max-width: 768px) {
    .card-container {
        flex-direction: column;
        gap: 16px;
    }

    .user-card,
    .balance-card {
        width: 100%;
        min-width: unset;
    }

    .user-header {
        padding: 16px;
        flex-direction: column;
        text-align: center;
    }

    .avatar-container {
        margin-right: 0;
        margin-bottom: 16px;
    }

    .user-basic-info {
        width: 100%;
    }

    .info-row {
        flex-direction: column;
        align-items: center;
        text-align: center;
    }

    .info-label {
        width: 100%;
        margin-bottom: 4px;
    }

    .bets-header {
        padding: 16px;
    }
}
</style>
